Restricting content is pretty popular when it comes to membership sites. With a WordPress shortcode, we can restrict access to the content. We don’t have to restrict access to the whole site, we can just restrict one part of the content. In this tutorial, we will create a WordPress shortcode that will do just that.

With the code we will create in this tutorial, you can restrict your own content even if you are using some other membership plugin. I’ll show you a simple example of that. But first, we need to create our WordPress shortcode.

The Basic Shortcode

To start with the code, we will create a simple shortcode.

With this code, we will display the content if the user can view it & if there is any content provided. To check if the user can view the content, we are using the function ibenic_can_user_view. Let’s define it:

This function will return only true if the user is logged in. We have also made this function extensible because we have used the function apply_filters. If you want to learn more on how to make plugins extensible, you can read my other article about 5 Ways to Make your WordPress Plugin Really Extensible.

Displaying a Message instead of the Content

With our function & shortcode in place, we can now wrap some content like this:

For now, if a user can’t view the content, they won’t see anything. A better way to handle this would be by displaying a message to them. In a real-world scenario, you could add a form here to login or maybe subscribe to your newsletter. For the purpose of this tutorial, we will just show a simple message.

Extending the Functionality

To extend the functionality, we could check the users’ role or something else. You can refactor the function that we use to check if a user can view it. But, since we have made it extensible, we will use the filter to change the returned value (true/false).

Let’s see now two examples:

S2 Member

Paid Membership Pro

In both of the examples above, we are using specific functions of the plugins. If those functions return true, the user will be able to view he content. We are also checking if those functions do exist. Why? Because we don’t want our plugin to break the site just because a plugin that requires this to function got deactivated or deleted.

Conclusion

WordPress Shortcode can be used for various functionalities. The code we created here is a simple showcase on how a WordPress shortcode can be powerful with just a few lines of code. To learn more about the Shortcode API, please consider reading the Codex.

Have you ever written your own shortcode? Share your experience or what you have built in the comments below:)

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.